﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MetroCashLib;
using MetroCashLib.DataBussiness;
using MetroCashLib.DataInfo;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace MetroCash
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class PAccountDetail : MetroCash.Common.LayoutAwarePage
    {
        private Account parentAccount;
        private Account currentAccount;
        private string profileName;
        public PAccountDetail()
        {
            this.InitializeComponent();

            Loaded += PAccountDetail_Loaded;
        }

        void PAccountDetail_Loaded(object sender, RoutedEventArgs e)
        {
            List<Currency> currencies = BCurrency.getAllCurrencies(this.profileName);
            this.cbbAccountCurrency.DataContext = currencies;
            if (this.currentAccount == null)
            {
                foreach (Currency c in currencies)
                {
                    if (c.Id == this.parentAccount.CurrencyId)
                    {
                        this.cbbAccountCurrency.SelectedItem = c;
                        break;
                    }
                }
            }
            else
            {
                foreach (Currency c in currencies)
                {
                    if (c.Id == this.currentAccount.CurrencyId)
                    {
                        this.cbbAccountCurrency.SelectedItem = c;
                        break;
                    }
                }
            }

            List<AccountType> allowedTypes = BAccountType.getAllowedAccountTypes(this.profileName, this.parentAccount);
            this.lstAccountTypes.DataContext = allowedTypes;
            if (this.currentAccount == null)
            {
                this.lstAccountTypes.SelectedIndex = 0;
            }
            else
            {
                foreach (AccountType ac in allowedTypes)
                {
                    if (ac.Id == this.currentAccount.AccountTypeId)
                    {
                        this.lstAccountTypes.SelectedItem = ac;
                        break;
                    }
                }
            }

            this.txtAccountParent.Text = this.parentAccount.Name;

            if (this.currentAccount == null)
            {
                this.pageTitle.Text = "Create new account";
                (this.btnFinish.Content as TextBlock).Text = "Create";

            }
            else
            {
                this.pageTitle.Text = "Update account: " + this.currentAccount.Name;
                (this.btnFinish.Content as TextBlock).Text = "Update";
                this.txtAccountName.Text = this.currentAccount.Name;
                this.chkIsPlaceHolder.IsChecked = this.currentAccount.isPlaceHolder;
                this.txtAccountDesciption.Text = this.currentAccount.Description;
            }
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (navigationParameter != null)
            {
                Account[] accounts = navigationParameter as Account[];
                this.parentAccount = accounts[0];
                this.currentAccount = accounts[1];
            }
            Windows.Storage.ApplicationDataContainer roamingSetting = Windows.Storage.ApplicationData.Current.RoamingSettings;
            if (roamingSetting.Values[SettingStrings.ROAMING_CURRENT_PROFILE] != null)
            {
                this.profileName = roamingSetting.Values[SettingStrings.ROAMING_CURRENT_PROFILE].ToString();
            }
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void btnFinish_Click_1(object sender, RoutedEventArgs e)
        {
            if (this.currentAccount == null)
            {
                // create new account
                Account account = new Account()
                {
                    AccountParentId = this.parentAccount.Id,
                    AccountTypeId = (lstAccountTypes.SelectedItem as AccountType).Id,
                    CurrencyId = (cbbAccountCurrency.SelectedItem as Currency).Id,
                    Description = this.txtAccountDesciption.Text,
                    Name = this.txtAccountName.Text,
                    isPlaceHolder = this.chkIsPlaceHolder.IsChecked == true
                };
                int result = BAccount.createAccount(this.profileName, account);

            }
            else
            {
                // edit current account
                Account account = new Account()
                {
                    AccountParentId = this.parentAccount.Id,
                    AccountTypeId = (lstAccountTypes.SelectedItem as AccountType).Id,
                    CurrencyId = (cbbAccountCurrency.SelectedItem as Currency).Id,
                    Description = this.txtAccountDesciption.Text,
                    Name = this.txtAccountName.Text,
                    isPlaceHolder = this.chkIsPlaceHolder.IsChecked == true
                };
                account.Id = this.currentAccount.Id;
                int result = BAccount.updateAccount(this.profileName, account);
            }
            this.Frame.GoBack();
        }
    }
}
